package com.hotelvip.service.impl;

import com.hotelvip.common.exception.ServiceException;
import com.hotelvip.common.pojo.PageObject;
import com.hotelvip.dao.SysLogMapper;
import com.hotelvip.entity.SysLog;
import com.hotelvip.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * @author Lzh
 * @date 2020/11/30 17:46
 */
@Service
public class SysLogServiceImpl implements SysLogService {
    @Autowired
    private SysLogMapper sysLogMapper;

    /**
     * 日志写入
     * @param entity
     */
    @Async //异步切入点方法
    @Transactional(propagation = Propagation.REQUIRES_NEW)//独立运行
//    @RequiresPermissions("sys:log:insert")
    @Override
    public void saveObject(SysLog entity) {
        sysLogMapper.insertObject(entity);
    }
//    @RequiresPermissions("sys:log:delete")
    @Override
    public int deleteObjects(Integer... ids) {
        //1.参数校验
        if(ids==null||ids.length==0)
            throw new IllegalArgumentException("请先选择");
        //2.执行删除
        int rows= sysLogMapper.deleteObjects(ids);
        //3.返回结果
        if(rows==0)
            throw new ServiceException("记录可能已经不存在");
        return rows;
    }

    @Override
    public PageObject<SysLog> findPageObjects(Long pageCurrent, String username) {
        //1.参数校验
        if(pageCurrent==null||pageCurrent<1)
            throw new IllegalArgumentException("页码值不正确");//IllegalArgumentException 无效参数异常
        //2.基于条件查询总记录数并校验
        long rowCount= sysLogMapper.getRowCount(username);
        if(rowCount==0)
            throw new ServiceException("没有找到对应记录");//自定义异常要具备更好业务含义
        //3.查询当前页要呈现的用户日志行为信息
        int pageSize=5;
        long startIndex=(pageCurrent-1)*pageSize;
        List<SysLog> records= sysLogMapper.findPageObjects(username, startIndex, pageSize);
        return PageObject.setPageObject(rowCount, records, Long.valueOf(pageSize), pageCurrent);
    }
}
